package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyCommend;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.domain.TravelContent;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentMongoService;
import cn.wolfcode.luowowo.comment.service.ITravelCommentMongoService;
import cn.wolfcode.luowowo.common.query.TravelQueryObject;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annotation.LoginUser;
import cn.wolfcode.luowowo.website.annotation.RequiredLogin;
import cn.wolfcode.luowowo.website.util.UMEditorUploader;
import cn.wolfcode.luowowo.website.util.UploadUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.zookeeper.data.Id;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("travel")
public class TravelController {
    @Value("${file.dir}")
    private String dir;

    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IDestinationService destinationService;

    @Reference
    private ITravelService travelService;

    @Reference
    private ITravelCommentMongoService travelCommentMongoService;

    @RequestMapping("")
    public  String index(@ModelAttribute("qo") TravelQueryObject qo, Model model){
        model.addAttribute("pageInfo",travelService.queryForList(qo));
        return "travel/list";
    }
    @RequestMapping("input")
    @RequiredLogin//判断是否登入
    public  String input(Long id, Model model,@LoginUser UserInfo user){
        if (id!=null){
            Travel travel = travelService.selectByPrimaryKey(id);
//            判断当前作者不是是当前登入用户
             if(travel.getAuthor().getId().equals(user.getId())){
                 model.addAttribute("tv",travel);
             }
        }
        model.addAttribute("dests",destinationService.selectAll());
        return "travel/input";
    }
    @RequestMapping("getContent")
    @ResponseBody//查询游记内容
    public  Object getContent(Long id, Model model){
        TravelContent travelContent = travelService.selectTravelContent(id);
        return travelContent;
    }


    @RequestMapping("saveOrUpdate")
    @RequiredLogin//判断是否登入
    @ResponseBody
    public  Object saveOrUpdate(Travel entity, @LoginUser UserInfo user){
        //设置当前作者
        entity.setAuthor(user);
        return travelService.saveOrUpdate(entity);
    }

    @RequestMapping("detail")
    public  String detail(Long id, Model model){
        Travel travel = travelService.selectByPrimaryKey(id);
        Long destId = travel.getDest().getId();
        List<Destination> toasts = destinationService.listToasts(destId);

        model.addAttribute("detail",travel);
        //toasts
        model.addAttribute("toasts",toasts);
        //sds  点击量前3的攻略
        model.addAttribute("sds",strategyDetailService.listViewnumTop3(destId));
        //ts 点击量前3的游记
        model.addAttribute("ts",travelService.listViewnumTop3(destId));
        //list 改文章的所有评论
        model.addAttribute("list",travelCommentMongoService.listByTraveId(id));
        return  "travel/detail";
    }


    //评论游记
    @RequestMapping("commentAdd")
    @RequiredLogin//判断是否登入
    public  String commentAdd(TravelComment comment,Integer floor, @LoginUser UserInfo user,Model model){
        AjaxResult result = new AjaxResult();
        //填充用户数据
        comment.setUserId(user.getId());
        comment.setUsername(user.getNickname());
        comment.setHeadUrl(user.getHeadImgUrl());
        comment.setLevel(user.getLevel());
        travelCommentMongoService.save(comment);
        // c 评论对象
        model.addAttribute("c",comment);
        //floor
        model.addAttribute("floor",++floor);
        return "travel/commentTpl";
    }
    @RequestMapping("coverImageUpload")
    @ResponseBody
    public Object coverImageUpload(MultipartFile pic){
        return UploadUtil.upload(pic,dir);
    }
    //上传图片 拖拽上传图片
    @RequestMapping("/contentImage")
    @ResponseBody
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile, dir);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }
}
